/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.session;

import com.thienloc.omap.entity.BoSoTrungThuong;
import java.util.List;
import org.slf4j.LoggerFactory;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

/**
 *
 * @author GiangLT
 */
@Stateless
public class BoSoTrungThuongFacade extends AbstractFacade<BoSoTrungThuong> {

    @PersistenceContext(unitName = "omapPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public BoSoTrungThuongFacade() {
        super(BoSoTrungThuong.class);
    }

    public List<BoSoTrungThuong> findByMaTinh(String maTinh) {
        try {
            TypedQuery<BoSoTrungThuong> q = em.createNamedQuery("BoSoTrungThuong.findByMaTinh", BoSoTrungThuong.class);
            q.setParameter("maTinh", maTinh);
            q.setMaxResults(30);
            List<BoSoTrungThuong> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(BoSoTrungThuongFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public List<BoSoTrungThuong> findLatest(Integer param) {
        try {
            String query = null;
            if (param == 0) {
                query = "SELECT * FROM bo_so_trung_thuong a where trunc(a.ngay_quay) = trunc(sysdate)";
            } else {
                query = "SELECT * FROM bo_so_trung_thuong a where trunc(a.ngay_quay) = trunc(sysdate - 1)";
            }
            Query q = em.createNativeQuery(query, BoSoTrungThuong.class);
            List<BoSoTrungThuong> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(BoSoTrungThuongFacade.class.getName()).error("", ex);
            return null;
        }
    }
}
